package com.lagou.springbootredis.controller;

import com.lagou.springbootredis.factory.JedisClusterFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisCluster;

import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/redis")
public class RedisController {

    @GetMapping("/put")
    public String put(@RequestParam(required = true) String key, @RequestParam(required = true) String value) {
        JedisCluster jedisCluster= JedisClusterFactory.getInstance().getJedisCluster();
        // 设置过期时间为10秒
        // NX是不存在时才set， XX是存在时才set， EX是秒，PX是毫秒
        jedisCluster.set(key,value,"NX","EX",10);
        return "Success";
    }

    @GetMapping("/get")
    public String get(@RequestParam(required = true) String key){
        JedisCluster jedisCluster= JedisClusterFactory.getInstance().getJedisCluster();
        String str = jedisCluster.get(key);
        return str;
    }
}
